# 使用库进行同步

from collections import defaultdict
from datetime import datetime, timedelta
import os
import time
from aligo import Aligo

# 本地不上传的文件
ignore = [".git", ".idea", ".DS_Store"]
# 网盘文件 
ALI_FOLDER_NAME = "Documents"

# 同步本地文件
LOCAL_FOLDER_NAME = "/Users/weizhou/Documents"

update_file_list = defaultdict(bool)
add_file_list = defaultdict(bool)

#  遍历ali全部文件
def getAliFiles(folderId):
    ll = ali.get_file_list(folderId)  # 获取网盘目录文件列表

    # 遍历文件列表
    for file in ll:
        print(file.file_id, file.name, file.updated_at)
        if file.type == "folder":
            getAliFiles(file.file_id)


# 判断文件
def hasFile(list, fileName):
    for file in list:
        if file.name == fileName:
            return file

    return None


#  遍历本地文件LOCAL
def getLocalFiles(path, folderId):
    # 本地文件列表
    path_list = os.listdir(path)

    # 同步获取阿里云文件
    ll = ali.get_file_list(folderId)

    for path_item in path_list:
        #  排除一些不用上传的文件
        if any(name in path_item for name in ignore) is False:
            print(path, path_item)

            # 云盘中是否有该文件
            aliFile = hasFile(ll, path_item)

            # 本地文件路径
            full_path = os.path.join(path, path_item)
            if aliFile is not None:
                if os.path.isfile(full_path):
                    ali_updated_at = datetime.strptime(
                        aliFile.updated_at, "%Y-%m-%dT%H:%M:%S.%fZ")+timedelta(hours=8)
                    ali_update_at = time.mktime(ali_updated_at.timetuple())
                    # print("云盘文件修改时间：", ali_update_at)
                    local_update_at = os.path.getatime(full_path)
                    

                    # 假如本地更新时间 大于 云盘更新时间 需要重新上传
                    if local_update_at > ali_update_at:
                        # print("本地文件修改时间：", local_update_at)
                        update_file_list[full_path] = True
                        
                        print("更新文件：", aliFile.name,
                              local_update_at, ali_update_at)
                        # 覆盖原文件
                        ali.upload_file(full_path, folderId,
                                        aliFile.name, None, "overwrite")

                else:
                    getLocalFiles(full_path, aliFile.file_id)
            else:
                # print("云盘没有", path_item)
                if os.path.isfile(full_path):
                    print("上传文件：", full_path)
                    ali.upload_file(full_path, folderId)
                    add_file_list[full_path] = True
                else:
                    print("创建文件夹", path_item)
                    ali.create_folder(path_item, folderId)
                    getLocalFiles(full_path, folderId)


if __name__ == '__main__':
    ali = Aligo()  # 第一次使用，会弹出二维码，供扫描登录
    # user = ali.get_user()  # 获取用户信息
    # getAliFiles(ALI_FOLDER_ID)  # 遍历阿里云文件

    documents = ali.get_folder_by_path(ALI_FOLDER_NAME)
    start_time = time.time()
    getLocalFiles(LOCAL_FOLDER_NAME, documents.file_id)  # 遍历本地文件
    print("更新本地文件", len(update_file_list))
    print("上传本地文件", len(add_file_list))
    print("同步耗时(分钟)：", (time.time() - start_time)/60 )
    
